package com.edgar.music.artist;

import android.support.annotation.StringRes;

import com.edgar.music.Constant;
import com.edgar.music.R;
import com.edgar.music.artist.entity.ArtistCategory;
import com.edgar.music.artist.entity.ArtistList;
import com.edgar.music.network.RequestCallback;
import com.edgar.music.network.RequestHelper;
import com.edgar.music.artist.entity.Artist;
import com.edgar.music.artist.type.LanguageType;
import com.edgar.music.artist.type.SingerCategoryType;

import java.util.ArrayList;
import java.util.List;

import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by Edgar on 2017/1/2.
 * 歌手管理
 */
public class ArtistManager {

    private ArtistApiService mArtistApiService;
    private RequestHelper mRequestHelper;
    private List<ArtistCategory> mArtistCategoryList;
    private static ArtistManager instance = new ArtistManager();

    private ArtistManager(){
        mRequestHelper = RequestHelper.getInstance();
        mArtistApiService = mRequestHelper.getApiService(ArtistApiService.class);
    }

    public static ArtistManager getInstance() {
        return instance;
    }

    /**
     * 获取歌手列表
     * @param languageType 语言类型
     * @param singerCategoryType 歌手分类
     * @param abc abc
     * @param offset 数据偏移量
     * @param limit 数据总量
     */
    public Call<ArtistList> getArtistList(LanguageType languageType, SingerCategoryType singerCategoryType
            , String abc, int offset, int limit, final RequestCallback<ArtistList> callback){
        Call<ArtistList> listCall = mArtistApiService.getArtistList(
                ArtistApiService.ARTIST_LIST_METHOD,languageType.getType(),singerCategoryType.getType(),abc,limit,offset,Constant.JSON_FORMAT);
        listCall.enqueue(new Callback<ArtistList>() {
            @Override
            public void onResponse(Response<ArtistList> response, Retrofit retrofit) {
                mRequestHelper.handlerSuccess(response,callback);
            }

            @Override
            public void onFailure(Throwable t) {
                mRequestHelper.handlerThrowable(t,callback);
            }
        });
        return listCall;
    }

    /**
     * 获取歌手详情
     * @param tingUid 歌手tingUid
     * @param callback 回调
     * @return 返回Call对象
     */
    public Call<Artist> getArtistDetails(long tingUid, final RequestCallback<Artist> callback){
        Call<Artist> artistDetailsCall = mArtistApiService.getArtistDetails(ArtistApiService.ARTIST_DETAILS,
                tingUid,Constant.JSON_FORMAT);
        artistDetailsCall.enqueue(new Callback<Artist>() {
            @Override
            public void onResponse(Response<Artist> response, Retrofit retrofit) {
                mRequestHelper.handlerSuccess(response,callback);
            }

            @Override
            public void onFailure(Throwable t) {
                mRequestHelper.handlerThrowable(t,callback);
            }
        });
        return artistDetailsCall;
    }

    private ArtistCategory newHotArtistCategory(@StringRes int title,LanguageType languageType, SingerCategoryType singerCategoryType){
        return new ArtistCategory().title(title).language(languageType).singerCategory(singerCategoryType).hot();
    }

    private void initArtistCategoryList(){
        if (mArtistCategoryList == null){
            mArtistCategoryList = new ArrayList<>();

            mArtistCategoryList.add(newHotArtistCategory(R.string.hot_artist,
                    LanguageType.NONE,SingerCategoryType.NONE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.chinese_male,
                    LanguageType.Chinese,SingerCategoryType.MALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.chinese_female,
                    LanguageType.Chinese,SingerCategoryType.FEMALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.chinese_band,
                    LanguageType.Chinese,SingerCategoryType.BAND));

            mArtistCategoryList.add(newHotArtistCategory(R.string.western_male,
                    LanguageType.Westerners,SingerCategoryType.MALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.western_female,
                    LanguageType.Westerners,SingerCategoryType.FEMALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.western_band,
                    LanguageType.Westerners,SingerCategoryType.BAND));

            mArtistCategoryList.add(newHotArtistCategory(R.string.korea_male,
                    LanguageType.Korea,SingerCategoryType.MALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.korea_female,
                    LanguageType.Korea,SingerCategoryType.FEMALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.korea_band,
                    LanguageType.Korea,SingerCategoryType.BAND));

            mArtistCategoryList.add(newHotArtistCategory(R.string.japan_male,
                    LanguageType.Japan,SingerCategoryType.MALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.japan_female,
                    LanguageType.Japan,SingerCategoryType.FEMALE));
            mArtistCategoryList.add(newHotArtistCategory(R.string.japan_band,
                    LanguageType.Japan,SingerCategoryType.BAND));
        }
    }

    /**
     * 获取歌手分类列表
     */
    public List<ArtistCategory> getArtistCategoryList(){
        initArtistCategoryList();
        return mArtistCategoryList;
    }
}